#define __DEBUG_C__
#include "debug.h"
#include "key.h"
#include "ioport.h"
#include "useruart2.h"
#include "gpt.h"
#include "pcinterface.h"
#include "at24c16.h"
#include "iirfilter.h"
unsigned char tempbuf[100]={1,2,3,4,5,6,7,8,9,0};
S16 debufbuf[1410];
U16 debugcnt=0;

//extern U8 debugduty;
unsigned short address,datl=2;
extern U16 time;

void delay(unsigned int count)
{
		unsigned int cnt;
		cnt = count;
		while(cnt>0)
		{
			cnt--;
		}
}
void debug_pwm(void)
{
	while(1)
	{
		
		Interface();
		switch(get_key())
		{
			case ON_KEY:  
			{
if(PFC_run != PFC_START)
{
open_pfc();

		PFC_run = PFC_START;
		gpt_init();
		delay(1000);
		open_pwm(100);
		delay(10000);
		time+=10;
		if(time>F_PWM-20)time=F_PWM-20;
		set_pwm1(F_PWM-20);
		set_pwm2(F_PWM-20);
		realy_con_l();
}
else
{
		PFC_run = PFC_STO;
		stop_gpt23();
		stop_pwm();
		PFC_run = 0x00;
		GPT_run = 0x00;
		FRE_DUTY_run = 0x00;
}
//				led_on(1);
//				open_pwm(100);
			}
			break;
			case FUNC_KEY: 
			{
if(PFC_run == PFC_START)
{
	if(GPT_run != GPT_START)
	{
			gpt23_init();
			Roll_led = 0;
			delay(10000);
			GPT_run = GPT_START;
			led_on_v1(2);
			open_gpt23();	
			FRE_DUTY_run = FRE_START;
			led_on_v1(3);
			led_off_v1(4);
			Max_interval_data = Cycle_data/2 - DEAD_TIME;
	}
	else
	{
		if(FRE_DUTY_run == FRE_START)
		{
			FRE_DUTY_run = DUTY_START;
			led_off_v1(3);
			led_on_v1(4);
		}
		else
		{
			FRE_DUTY_run = FRE_START;
			led_on_v1(3);
			led_off_v1(4);
		}
		
	}
}
			}
			break;
			case UP_KEY: 
			{
	enable_wgpt2();
	enable_wgpt3();
if(FRE_DUTY_run == FRE_START)
{
	Cycle_data+=20;
	if(Cycle_data > 65500)
	{
		Cycle_data = 65500;
	}				
	GPT3.GTST.BIT.TCFPO = 0;
	while(!GPT3.GTST.BIT.TCFPO)
	{
		;
	}
	GPT3.GTST.BIT.TCFPO = 0;
	GPT2.GTPDBR = Cycle_data;
	GPT3.GTPDBR = Cycle_data;
	GPT3.GTCCRC = Cycle_data/2;
	GPT2.GTCCRC = Cycle_data/2;
}
if(FRE_DUTY_run == DUTY_START)
{
	if(Interval_data < (Max_interval_data - 30))
	{
		Interval_data += 10;
		GPT3.GTDBD = DEAD_TIME + Interval_data;
//		GPT3.GTDBU = DEAD_TIME + Interval_data;
	}
}

	disable_wgpt2();
	disable_wgpt3();
/*				 led_roll(3);
				 time+=10;
				 if(time>F_PWM-20)time=F_PWM-20;
				 set_pwm1(F_PWM-20);
				 set_pwm2(F_PWM-20);
				 realy_con_l();
*/
			}
			break;
			case DOWN_KEY: 
			{
				enable_wgpt2();
				enable_wgpt3();
if(FRE_DUTY_run == FRE_START)
{
	Cycle_data-=20;
	if(Cycle_data < 300)
	{
		Cycle_data = 300;
	}				
	GPT3.GTST.BIT.TCFPO = 0;
	while(!GPT3.GTST.BIT.TCFPO)
	{
		;
	}
	GPT3.GTST.BIT.TCFPO = 0;
	GPT2.GTPDBR = Cycle_data;
	GPT3.GTPDBR = Cycle_data;				
	GPT3.GTCCRC = Cycle_data/2;
	GPT2.GTCCRC = Cycle_data/2;
	if((Interval_data + 70) > (Cycle_data/2))
	{
			Interval_data -= 10;
			GPT3.GTDBD = DEAD_TIME + Interval_data;
			GPT3.GTDBU = DEAD_TIME + Interval_data;
	}
}
if(FRE_DUTY_run == DUTY_START)
{
	if(Interval_data > 30)
	{
		Interval_data -= 10;
		GPT3.GTDBD = DEAD_TIME + Interval_data;
//		GPT3.GTDBU = DEAD_TIME + Interval_data;
	}
}

	disable_wgpt2();
	disable_wgpt3();
/*				led_roll(4);
				 time-=10;
				 realy_con_h();
				 if(time<20)time=20;
				 set_pwm1(0);
				 set_pwm2(0);
*/			}
			break;
		}
	}	
}
void debug_uart(void)
{
	while(1)
	{
		switch(get_key())
		{
			case ON_KEY:  
			{
				send_dat_to_uart2(tempbuf,10);
				led_roll(1);break;
			}
			break;
			case FUNC_KEY: 
			{
				led_roll(2);
			}
			break;
			case UP_KEY: 
			{
				 led_roll(3);
			}
			break;
			case DOWN_KEY: 
			{
				led_roll(4);
			}
			break;
		}
	}
}
void debug_eeprom(void)
{
	while(1)
	{
		write_24c16(address,tempbuf,datl);
		read_24c16(address,tempbuf+10,datl);
	}	
}

static S16 tempduty;

void debug_adcvac(U16 ad)
{
	debufbuf[debugcnt]=ad;
	debugcnt++;
	if(debugcnt>1400)
	{
		debugcnt=0;
	}
}

void test_iir_f_i(void)
{
	U16 i;
	for(i=0;i<1000;i++)
	{
//		filter_pro_mov(&iirfcs1);
//		iirfcs1.adin=sin(((float)((3.14/300)*i))+1.57)-10;//+sin(((float)((3.14/100)*i)));
//		fliter_pro_yn(&iirfcs1);
//		PWM_out(iirfcs1.Yn);
		debufbuf[i]=tempduty;
	}
}
void debug_filteri(void)
{
	iir_filter_init();
	test_iir_f_i();
	while(1);
}